package com.yida.gwj.vote.controller;


import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.dto.GwjGVoteResultInnerDTO;
import com.yida.gwj.vote.domain.dto.*;
import com.yida.gwj.vote.domain.request.GwjVoteResultDetailQueryRequest;
import com.yida.gwj.api.domain.request.GwjVoteResultInnerRequest;
import com.yida.gwj.vote.domain.vo.GwjVoteResultDetailExcelVO;
import com.yida.gwj.vote.service.IGwjVoteResultDetailService;
import com.yida.gwj.vote.service.IGwjVoteStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lqq
 * @date 2023-03-14
 */
@Api(tags = "投票结果详情")
@RestController
@RequestMapping("/detail")
public class GwjVoteResultDetailController extends BaseController {

    @Resource
    private IGwjVoteResultDetailService voteResultDetailService;

    @Resource
    private IGwjVoteStatisticsService voteStatisticsService;

    @ApiOperation(value = "查询电子投票记录", response = GwjEVoteResultDetailListDTO.class)
    @RequiresPermissions("gwj:vote:e:project:statistics")
    @GetMapping("/e/list")
    public TableDataInfo eList(@Validated GwjVoteResultDetailQueryRequest request) {
        startPage();
        List<GwjEVoteResultDetailListDTO> list = voteResultDetailService.selectEVoteResultDetailDTOList(request);
        return getDataTable(list);
    }

    @ApiOperation(value = "查询公维金投票记录", response = GwjGVoteResultDetailListDTO.class)
    @RequiresPermissions("gwj:vote:g:project:statistics")
    @GetMapping("/g/list")
    public TableDataInfo gList(@Validated GwjVoteResultDetailQueryRequest request) {
        startPage();
        List<GwjGVoteResultDetailListDTO> list = voteResultDetailService.selectGVoteResultDetailDTOList(request);
        return getDataTable(list);
    }

    @ApiOperation("导出投票记录")
    @RequiresPermissions("gwj:vote:g:detail:export")
    @Log(title = "公维金内网投票-导出投票记录", businessType = BusinessType.EXPORT, systemName = "vote")
    @PostMapping("/export/{voteId}")
    public void export(HttpServletResponse response, @PathVariable Long voteId) {
        List<GwjEVoteResultDetailListDTO> list = voteResultDetailService.selectEVoteResultDetailDTOList(new GwjVoteResultDetailQueryRequest() {{
            setVoteId(voteId);
        }});
        ExcelUtil<GwjVoteResultDetailExcelVO> util = new ExcelUtil<>(GwjVoteResultDetailExcelVO.class);
        util.exportExcel(response, BeanUtil.copyToList(list, GwjVoteResultDetailExcelVO.class), "投票结果详情数据");
    }

    @ApiOperation(value = "查询电子投票票数详细", response = GwjVoteResultDetailItemDTO.class)
    @RequiresPermissions("gwj:vote:e:project:statistics")
    @GetMapping("/e/count/{voteId}")
    public AjaxResult eDetailCount(@PathVariable Long voteId) {
        return AjaxResult.success(voteStatisticsService.voteResultDetailStatistics(voteId));
    }

    @ApiOperation(value = "查询公维金投票票数详细", response = GwjVoteResultDetailOptionCountDTO.class)
    @RequiresPermissions("gwj:vote:g:project:statistics")
    @GetMapping("/g/count/{voteId}")
    public AjaxResult gDetailCount(@PathVariable Long voteId) {
        return AjaxResult.success(voteStatisticsService.voteResultDetailStatistics(voteId));
    }

    @InnerAuth
    @PostMapping("/g/voteResult")
    public R<List<GwjGVoteResultInnerDTO>> innerVoteResult(@RequestBody GwjVoteResultInnerRequest request) {
        return voteResultDetailService.innerVoteResult(request);
    }
}
